//
//  PracticePartnerViewController.m
//  CoachsBook
//
//  Created by huiyong li on 2022/8/18.
//

#import "PracticePartnerViewController.h"
#import "OneTabButView.h"
#import "CoachsBookInputModel.h"
#import "CoachsBookTextFieldCell.h"
#import "CoachsBookRadioCell.h"
#import "CoachsBookTextViewCell.h"
#import "CoachsBookSwitchCell.h"
#import "CoachsBookTagViewCell.h"

@interface PracticePartnerViewController ()<UITableViewDelegate,UITableViewDataSource,OneTabButViewDelegate,UITextFieldDelegate>

@end

@implementation PracticePartnerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navgationheadv.titlelabel.text = @"陪练服务";
    OneTabButView *tabbutview = [[OneTabButView alloc]initWithFrame:CGRectMake(0, APPheight-SafeAreaBottomHeight-65, APPwidth, 65)];
    tabbutview.delegate = self;
    [tabbutview.tabbut setTitle:@"保存" forState:UIControlStateNormal];
    [self.view addSubview:tabbutview];
    
    [self getData];
}

- (void)getData
{
    NSArray *titlearr = @[];
    NSArray *promptarr = @[];
    NSArray *unitarr = @[];
    NSArray *celltypearr = @[];
    NSArray *contentarr = @[];
    

    titlearr = @[@"陪练服务开关",@"陪练类型",@"陪练价格",@"陪练车型",@"起约时长",@"是否提供陪练车",@"汽车品牌",@"陪练服务简介"];
    promptarr = @[@"",@"",@"输入整数金额",@"",@"输入整数",@"",@"请选择",@"请输入简介内容"];
    unitarr = @[@"",@"",@"元/小时",@"",@"/小时",@"",@"",@""];
    celltypearr = @[@"5",@"7",@"1",@"2",@"1",@"2",@"1",@"4"];
    contentarr =  @[@"1",@"",@"",@"1",@"",@"1",@"",@""];
    if (self.dataArray.count) {
        CoachsBookInputModel *model = self.dataArray.firstObject;
        if ([model.content isEqualToString:@"0"]) {
            [self.dataArray removeAllObjects];
            titlearr = @[@"陪练服务开关"];
            promptarr = @[@""];
            unitarr = @[@""];
            celltypearr = @[@"5"];
            contentarr =  @[@"0"];
        }
        else{
            [self.dataArray removeAllObjects];
        }
    }
    
    for (NSInteger i=0; i<titlearr.count; i++) {
        CoachsBookInputModel *model = [[CoachsBookInputModel alloc]init];
        model.title = titlearr[i];
        model.prompt = promptarr[i];
        model.unit = unitarr[i];
        model.cellType = [celltypearr[i] integerValue];
        model.content = contentarr[i];
        model.showArrow = NO;
     if (i==3)
        {
            model.leftTitle = @"自动挡";
            model.righTitle = @"手动挡";
        }
        else if (i==5)
        {
            model.leftTitle = @"是";
            model.righTitle = @"否";
        }
        else if (i==6)
        {
            model.showArrow = YES;
        }
        else if (i==1)
        {
            model.tagArr = @[@"科二陪练",@"科三陪练",@"拿本上路陪练"];
        }
        [self.dataArray addObject:model];
    }
    [self.tableView reloadData];

}

- (void)setupUI {
    [super setupUI];
    self.tableView.frame = CGRectMake(0, SafeAreaTopHight, APPwidth, APPheight-SafeAreaTopHight-SafeAreaBottomHeight-110);
    
    UIView *HeaderV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, APPwidth, 45)];
    HeaderV.backgroundColor = [UIColor whiteColor];
    UIView *myDataLineV = [[UIView alloc]initWithFrame:CGRectMake(15, 15, 3, 15)];
    myDataLineV.backgroundColor = rgba(22, 120, 255, 1);
    myDataLineV.layer.cornerRadius = 2;
    [HeaderV addSubview:myDataLineV];
    UILabel *myDataLab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(myDataLineV.frame)+5, 15, 150, 15)];
    myDataLab.text = @"详细信息";
    myDataLab.textColor = [UIColor blackColor];
    myDataLab.textAlignment = 0;
    myDataLab.font = kPingFangSemibold(16);
    [HeaderV addSubview:myDataLab];
    
    self.tableView.tableHeaderView = HeaderV;
    

}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.dataArray.count-1) {
        return 260;
    }
    else if (indexPath.row == 1)
    {
        CoachsBookInputModel *model = self.dataArray[indexPath.row];
        return [LongHelper createTagButtonToArr:model.tagArr WithViewWidth:APPwidth-32 WithwidthNumber:3]+45+15;
    }
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CoachsBookInputModel *model = self.dataArray[indexPath.row];
    if (model.cellType == 1) {
        CoachsBookTextFieldCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookTextFieldCell"];
        if (!cell) {
            cell=[[CoachsBookTextFieldCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookTextFieldCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell cellFillWithModel:model];
        cell.contentField.tag = indexPath.row;
        cell.contentField.delegate = self;
        return cell;
    }
    else if (model.cellType == 2)
    {
        CoachsBookRadioCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookRadioCell"];
        if (!cell) {
            cell=[[CoachsBookRadioCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookRadioCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell cellFillWithModel:model AndleftTitle:model.leftTitle righTitle:model.righTitle];
        [cell.lefttabbut addTarget:self action:@selector(lefttabbutCheng:) forControlEvents:UIControlEventTouchUpInside];
        [cell.righttabbut addTarget:self action:@selector(righttabbutCheng:) forControlEvents:UIControlEventTouchUpInside];
        cell.lefttabbut.tag = cell.righttabbut.tag = indexPath.row;
        return cell;
    }
    else if (model.cellType == 4)
    {
        CoachsBookTextViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookTextViewCell"];
        if (!cell) {
            cell=[[CoachsBookTextViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookTextViewCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell cellFillWithModel:model];
        cell.CertificationTextViewBlock = ^(NSString * _Nonnull textViewText) {
            NSLog(@"99999=%@",textViewText);
            model.content = textViewText;
            [self.tableView reloadData];
        };
        return cell;
    }
    else if (model.cellType == 5)
    {
        CoachsBookSwitchCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookSwitchCell"];
        if (!cell) {
            cell=[[CoachsBookSwitchCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookSwitchCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell cellFillWithModel:model];
        [cell.SwitchBut addTarget:self action:@selector(SwitchButCheng:) forControlEvents:UIControlEventValueChanged];
        cell.SwitchBut.tag = indexPath.row;
        return cell;
    }
    CoachsBookTagViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookTagViewCell"];
    if (!cell) {
        cell=[[CoachsBookTagViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookTagViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell cellFillWithModel:model];
    cell.setTagViewTitleArrBlock = ^(NSArray *tagstrarr) {
        NSString *tagstr = [tagstrarr componentsJoinedByString:@","];
        NSLog(@"tagstr=%@",tagstr);
        model.content = tagstr;
    };
    return cell;
    
}

#pragma mark -陪练服务开关
-(void)SwitchButCheng:(UISwitch *)but
{
    CoachsBookInputModel *model = self.dataArray[but.tag];
    if (but.on) {
        model.content = @"1";
        [LongHelper poppingAlertWithReminderTitle:@"" message:@"陪练服务已关闭，学员将不能预约您的陪练服务" AndActionTitle:@"我知道了" ActionHandler:^(UIAlertAction *action) {
        } ViewController:self];
    }
    else{
        model.content = @"0";
        [LongHelper poppingAlertWithReminderTitle:@"" message:@"陪练服务已打开，学员将会通过您发布陪练服务预约" AndActionTitle:@"我知道了" ActionHandler:^(UIAlertAction *action) {
        } ViewController:self];
    }
    [self getData];
}

#pragma mark -单选按钮
-(void)lefttabbutCheng:(UIButton *)but
{
    CoachsBookInputModel *model = self.dataArray[but.tag];
    model.content = @"1";
    [self.view endEditing:YES];
    NSIndexPath *  indexPath = [NSIndexPath indexPathForRow:but.tag inSection:0];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

-(void)righttabbutCheng:(UIButton *)but
{
    CoachsBookInputModel *model = self.dataArray[but.tag];
    model.content = @"2";
    [self.view endEditing:YES];
    NSIndexPath *  indexPath = [NSIndexPath indexPathForRow:but.tag inSection:0];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:( UITextField*)textField
{
    CoachsBookInputModel *model = self.dataArray[textField.tag];
    if (model.showArrow) {
        UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        textField.inputView = dummyView; // Hide keyboard,but show blinking cursor
        NSMutableArray *PickerViewModelArr = [[NSMutableArray alloc]init];
        [PickerViewModelArr removeAllObjects];
        if (textField.tag == 1) {

        }
        else if (textField.tag == 2)
        {
//            [self.view endEditing:YES];
        }
    }
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    CoachsBookInputModel *model = self.dataArray[textField.tag];
    if (textField.text.length) {
        model.content = textField.text;
    }
}

#pragma mark - CYWKLPickerViewDelegate
-(void)choosPickerView:(UIPickerView *)pickerView WithRowStr:(NSString *)rowStr AndRowNum:(NSInteger)row
{
    CoachsBookInputModel *model = self.dataArray[1];
    model.content = rowStr;
    NSIndexPath *  indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
    CoachsBookTextFieldCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.contentField.text = rowStr;
    [self.view endEditing:YES];
    
}

-(void)closePickerView:(UIPickerView *)pickerView
{
    [self.view endEditing:YES];
}


#pragma mark -OneTabButViewDelegate(保存)
- (void)OneTabButViewBtnSelectedDelegate:(NSInteger)tag
{
    
}
@end
